﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace PaladoreQuestItem
{
    class Item
    {
        public string name;
        public element itemElement;
        public int rarity, level;
        public List<Pali> mobs = new List<Pali>();
        public Pali mob;
        public List<Quest> quest = new List<Quest>();


        public Item(string name, element element, int rarity, int level, string mobThatDrops, string quest)
        {
            this.name = name;
            this.itemElement = element;
            this.rarity = rarity;
            this.level = level;
            if (mobThatDrops != "")
            {
                //if it exists already, add the entry to this.list
                if (Form1.paliList.ContainsKey(mobThatDrops))
                {
                    //multiple items/mobs
                    //Form1.paliList[mobThatDrops].drops.Add(this);
                    //this.mobs.Add(Form1.paliList[mobThatDrops]);
                    //single item/mob
                    Form1.paliList[mobThatDrops].drop = this;
                    this.mob = Form1.paliList[mobThatDrops];
                }
                else    // if it doesnt exist, add it to the main list and add that entry to this.list
                {
                    //multiple items/mobs
                    //Form1.paliList.Add(mobThatDrops, new Pali(mobThatDrops));
                    //this.mobs.Add(Form1.paliList[mobThatDrops]);
                    //single item/mob
                    Form1.paliList.Add(mobThatDrops, new Pali(mobThatDrops)); //make new entry
                    Form1.paliList[mobThatDrops].drop = this;                 //make this item the drop
                    this.mob = Form1.paliList[mobThatDrops];                  //set this mob from the entry in main list
                }
            }
            if (quest != "")
            {
                if (Form1.questList.ContainsKey(quest))
                {
                    Form1.questList[quest].reward = this;
                    this.quest.Add(Form1.questList[quest]);
                }
                else
                {
                    Form1.questList.Add(quest, new Quest()); //todo: add constructor args Quest(qname)
                    this.quest.Add(Form1.questList[quest]);
                }
            }
        }

        public void setItemAttributes(string name, element element, int rarity, int level, string mobThatDrops, string quest)
        {
            this.name = name;
            this.itemElement = element;
            this.rarity = rarity;
            this.level = level;
            if (mobThatDrops != "")
            {
                //if it exists already, add the entry to this.list
                if (Form1.paliList.ContainsKey(mobThatDrops))
                {
                    Form1.paliList[mobThatDrops].drops.Add(this);
                    this.mobs.Add(Form1.paliList[mobThatDrops]);
                }
                else    // if it doesnt exist, add it to the main list and add that entry to this.list
                {
                    Form1.paliList.Add(mobThatDrops, new Pali(mobThatDrops));
                    this.mobs.Add(Form1.paliList[mobThatDrops]);
                }
            }
            if (quest != "")
            {
                if (Form1.questList.ContainsKey(quest))
                {
                    Form1.questList[quest].reward = this;
                    this.quest.Add(Form1.questList[quest]);
                }
                else
                {
                    Form1.questList.Add(quest, new Quest()); //todo: add constructor args Quest(qname)
                    this.quest.Add(Form1.questList[quest]);
                }
            }
        }

        public Item(string name)
        {
            this.name = name;
            //new Item(name, element.Unknown, 0, 0, "", ""); //caused a dictionary error sometimes, not sure why
        }

        public override string ToString() //todo: remove code for multiple, set with single
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Item Name:\t{0}\n", name);
            if (itemElement != element.Unknown) sb.AppendFormat("Item Element:\t{0}\n", itemElement);
            sb.AppendFormat("Item Rarity:\t{0}\nItem Level:\t{1}\n", rarity, level);
            if (quest.Count > 0) sb.AppendFormat("Quest:\t\t{0}\n", quest[0].ToString(PIQ.Item));
            if (mobs.Count > 0) sb.AppendFormat("Dropped By Pali:\n{1}", mobs[0].ToString(PIQ.Item));

            /*
            string format1, format2 = "";
            //format1 = string.Format("Item Name:\t{0}\nItem Element:\t{1}\nItem Rarity:\t{2}\nItem Level:\t{3}\n", name, itemElement, rarity, level);
            if (quest.Count > 0 && mobs.Count > 0) format2 = string.Format("Quest:\t{0}\nDropped By Pali:\n{1}", quest[0].ToString(PIQ.Item), mobs[0].ToString(PIQ.Item));
            else if (quest.Count > 0) format2 = string.Format("Quest:\t\t{0}\nDropper:\t\t{1}\n", quest[0].ToString(PIQ.Item), "Unknown");
            else if (mobs.Count > 0) format2 = string.Format("Quest:\t\t{0}\nDropped By Pali:\n{1}", "Unknown", mobs[0].ToString(PIQ.Item));
            else format2 = string.Format("Quest:\t\t{0}\nDropper:\t\t{1}\n", "Unknown", "Unknown");
            */
            string temp = sb.ToString();
            return temp;
        }
        public string ToString(PIQ sender) //todo: if sender==pali dostuff; if sender==quest do different
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Item Name:\t{0}\n", name);
            if (itemElement != element.Unknown) sb.AppendFormat("Item Element:\t{0}\n", itemElement);
            sb.AppendFormat("Item Rarity:\t{0}\nItem Level:\t{1}\n", rarity, level);
            if (quest.Count > 0) sb.AppendFormat("Quest:\t\t{0}\n", quest[0].ToString(PIQ.Item));
            if (mobs.Count > 0) sb.AppendFormat("Dropped By Pali:\n{1}", mobs[0].ToString(PIQ.Item));


            string format1, format2 = "";
            format1 = string.Format("\nItem Name:\t{0}\nItem Element:\t{1}\nItem Rarity:\t{2}\nItem Level:\t{3}\n", name, itemElement, rarity, level);
            if (quest.Count > 0 && mobs.Count > 0) format2 = string.Format("Quest:\t{0}\nDropped By Pali:\n{1}", quest[0].name, mobs[0].name);
            else if (quest.Count > 0) format2 = string.Format("Quest:\t\t{0}\nDropper:\t{1}\n", quest[0].name, "Unknown");
            else if (mobs.Count > 0) format2 = string.Format("Quest:\t\t{0}\nDropped By Pali:\n{1}", "Unknown", mobs[0].name);
            else format2 = string.Format("Quest:\t\t{0}\nDropper:\t{1}\n", "Unknown", "Unknown");

            return format1 + format2;
        }
    }
}
